package flc.ast.fragment;

import android.Manifest;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.StatFs;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.activity.ApkManageActivity;
import flc.ast.activity.AudioManageActivity;
import flc.ast.activity.DocManagerActivity;
import flc.ast.activity.FileCompressActivity;
import flc.ast.activity.FileDecoActivity;
import flc.ast.activity.FileProActivity;
import flc.ast.activity.PicManageActivity;
import flc.ast.activity.PicTransferActivity;
import flc.ast.activity.TransferActivity;
import flc.ast.activity.VideoManageActivity;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.device.StorageUtil;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llPicture.setOnClickListener(this);
        mDataBinding.llVideo.setOnClickListener(this);
        mDataBinding.llAudio.setOnClickListener(this);
        mDataBinding.llDocument.setOnClickListener(this);
        mDataBinding.llApp.setOnClickListener(this);
        mDataBinding.llFileTransfer.setOnClickListener(this);
        mDataBinding.llFilePro.setOnClickListener(this);
        mDataBinding.llFileDeco.setOnClickListener(this);
        mDataBinding.llFileCompress.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llPicture://图片管理
                startActivity(PicManageActivity.class);
                break;
            case R.id.llVideo://视频管理
                startActivity(VideoManageActivity.class);
                break;
            case R.id.llAudio://音频管理
                startActivity(AudioManageActivity.class);
                break;
            case R.id.llDocument://文档管理
                startActivity(DocManagerActivity.class);
                break;
            case R.id.llApp://应用管理
                startActivity(ApkManageActivity.class);
                break;
            case R.id.llFileTransfer://文件传输
                getPermission();
                break;
            case R.id.llFilePro://文件投屏
                startActivity(FileProActivity.class);
                break;
            case R.id.llFileDeco://文件解压
                startActivity(FileDecoActivity.class);
                break;
            case R.id.llFileCompress://文件压缩
                startActivity(FileCompressActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        StatFs statFs = StorageUtil.getInternalMemory();//内部存储
        //已使用
        long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
        //总内存
        long allLong = statFs.getTotalBytes();
        mDataBinding.tvUsable.setText(ConvertUtils.byte2FitMemorySize(allLong - usedLong, 0));
        mDataBinding.tvMemorise.setText(getString(R.string.memorise, ConvertUtils.byte2FitMemorySize(allLong, 0), ConvertUtils.byte2FitMemorySize(usedLong, 0)));
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_ALL_MEDIA, Manifest.permission.READ_CONTACTS, Manifest.permission.CAMERA)
                .reqPermissionDesc(getString(R.string.get_transfer_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(TransferActivity.class);
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        ToastUtils.showShort(getString(R.string.no_permission));
                    }
                }).request();
    }

}